﻿var address=new Array();

for(var k = 0;k < 30;k++)
    {
        address[k] = new Array();
    }
function Mask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "block"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "block";     
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "block";
    cart.style.position = "absolute";
    var bigImg = document.getElementById("SendMethod");
    var pleft = 0;
    var ptop = 0;
   
    var obj = bigImg.offsetParent;
 
    while(obj.tagName != "BODY")
    {
         pleft = pleft + obj.offsetLeft;
         ptop = ptop + obj.offsetTop;
         obj = obj.offsetParent;
    }
    ptop = ptop +50;
    cart.style.left = pleft +200+ "px";
    cart.style.top = ptop +"px";
    cart.style.width = 300 + "px";
    cart.style.backgroundColor  = "#ffffff";
}

function HidMask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "none"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "none";   
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "none";    
}
function SetAddress(data)
{    
    document.getElementById("ShoppingCart").innerHTML=data;
}
function GetWLList()
{
    document.getElementById("div_loadding").innerHTML = "<span ><img src='../Include/Images/loadimg.gif'></span>";
     $j.post("../Handler/AddressHandler.ashx", {OP:"WL"},
            function(data){                          
            document.getElementById("div_wl").innerHTML=data;
           document.getElementById("div_loadding").innerHTML ="";
            }
        );
}
function GetddlyList()
{
    document.getElementById("div_loadding").innerHTML = "<span ><img src='../Include/Images/loadimg.gif'></span>";
     $j.post("../Handler/AddressHandler.ashx", {OP:"ddly"},
            function(data){                          
            document.getElementById("div_ddly").innerHTML=data;
           document.getElementById("div_loadding").innerHTML ="";
            }
        );
}
function AddressInit(UserCode)
{
    Mask();
    SetAddress("<br /><br />数据加载中......<br /><br /><img src='../Include/Images/loadimg.gif'>");  
     $j.post("../Handler/AddressHandler.ashx", {UserCode:UserCode, OP:"Init"},
            function(data){                          
               eval(data);
               InitProvince();
               InitAddressList();
               HidMask();
            }
        );
}
function InitProvince()
{
    var options = address[0][0];
    var arr_option = options.split("&&");
    var sel = document.getElementById("Province");
    while( sel.length>0 )
    {
        sel.removeChild(sel.options(0));        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
}
function GetCityList(province,value)
{
    LoadCity=false;
    if(parseInt(province)>-1)
    {
        $j.post("../Handler/AddressHandler.ashx", {Province:province, OP:"City"},
                function(data){                                            
                   InitCity(data,value);                   
                }
            );
     }
}
function InitCity(data,value)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("City");  
    
    while(sel.length>0 )
    {
        sel.removeChild(sel.options(0));
       
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(value!=null)
    {        
        sel.value=value;
    }
    else
    {
        sel.value="-1";
        var areas = document.getElementById("Area"); 
        while( areas.length>0 )
        {
            areas.removeChild(areas.options(0));
        }
        areas.options[0]=new Option("请选择","-1");        
    }
    
}
function GetAreaList(value,selectvalue)
{
    LoadArea=false;
    if(parseInt(value)>-1)
    {
        $j.post("../Handler/AddressHandler.ashx", {City:value, OP:"Area"},
                function(data){                          
                   InitArea(data,selectvalue);
                }
            );
     }
     else
     {
        var sel = document.getElementById("Area"); 
    
        while( sel.length>0 )
        {
            sel.removeChild(sel.options(0));
            
        }
        sel.options[0]=new Option("请选择","-1");
     }
}
function InitArea(data,selectvalue)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("Area");  
    
    while( sel.length>0 )
    {
        sel.removeChild(sel.options(0));
        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(selectvalue!=null)
    {
        sel.value=selectvalue;
    }
    else
    {
        sel.value="-1";
    }
}
function InitAddressList()
{
    var up=false;
    var Tablehtml="<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#999999\">"
                               + "<tr style=\"background:#e7e7e7\">"
                               + "<td style=\"font-weight:bold;text-align:center;line-height:22px;\"> "
                               +"&nbsp;</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"收货人姓名</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"具体地址</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"邮编</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"固定电话</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"手机</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"选择地址</td>"
                               +" </tr>";
    for(var i=1;i<30;i++)
    {
        if(address[i][0]!=null)
        {
            Tablehtml=Tablehtml+"<tr style=\"background:#ffffff\">"
                                +"<td style=\"text-align:center;line-height:22px;\">地址"+i.toString()+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][2]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][4]+address[i][6]+address[i][8]+address[i][9]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][10]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][11]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][12]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\"><a href=\"Javascript:\" onclick=\"ShowlatestAddress("+address[i][0]+")\">选择</a></td>"
                                +"</tr>";
            up=true;
        }
    }
    if(up)
    {
        ShowlatestAddress(address[1][0]);
        document.getElementById("AddressList").innerHTML=Tablehtml;
    }
}
function ShowlatestAddress(ID)
{
    var Province=document.getElementById("Province");
    var City=document.getElementById("City");
    var Area=document.getElementById("Area");   
    for(var x=1;x<30;x++)
    {
        if(address[x][0]==ID)
        {
            document.getElementById("T_Name").value=address[x][2];
            document.getElementById("Address").value=address[x][9];
            document.getElementById("Post").value=address[x][10];
            document.getElementById("Phone").value=address[x][11];
            document.getElementById("Mobile").value=address[x][12]; 
            Province.value= address[x][3];
            GetCityList(address[x][3],address[x][5]);
            GetAreaList(address[x][5],address[x][7]);                   
        }
    }
    
}
function CheckAddress()
{
    var reciver=document.getElementById("T_Name");
    if(reciver.value.length<1)
    {
        alert("收货人姓名不能为空");
        reciver.focus();
        return false;
    }
//    var Province=document.getElementById("Province");
//    if(Province.selectedIndex<1)
//    {
//        alert("请选择省份");
//        Province.focus();
//        return false;
//    }
//    var City=document.getElementById("City");
//    if(City.selectedIndex<1)
//    {
//        alert("请选择城市");
//        City.focus();
//        return false;
//    }
//    var Area=document.getElementById("Area");
//    if(Area.selectedIndex<1)
//    {
//        alert("请选择区县");
//        Area.focus();
//        return false;
//    }
    var daddress=document.getElementById("Address");
    if(daddress.value.length<1)
    {
        alert("请填写具体地址");
        daddress.focus();
        return false;
    }
//    var post=document.getElementById("Post");
//    if(isNaN(post.value))
//    {
//        alert("邮编含有非法字符");
//        post.focus();
//        return false;
//    }
//    if(post.value.length!=6)
//    {
//        alert("邮编长度非法");
//        post.focus();
//        return false;
//    }
    var phone=document.getElementById("Phone");
    if(isNaN(phone.value))
    {
        alert("固定电话含有非法字符");
        phone.focus();
        return false;
    }
    var mobile=document.getElementById("Mobile");
    if(isNaN(mobile.value))
    {
        alert("手机号码含有非法字符");
        mobile.focus();
        return false;
    }
    if(mobile.value.length!=11)
    {
        alert("手机长度错误");
        mobile.focus();
        return false;
    }
    var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/;
    var flag=myreg.test(mobile.value);
    if(!flag)
    {
        alert("手机号码格式错误");
        mobile.focus();
        return false;
    }
    return true;
}
function AddNewAddress()
{    
    if(CheckAddress())
    {
        var reciver=document.getElementById("T_Name").value;
        var Province=document.getElementById("Province").value;
        var City=document.getElementById("City").value;
        var Area=document.getElementById("Area").value;
        var daddress=document.getElementById("Address").value;
        var post=document.getElementById("Post").value;
        var phone=document.getElementById("Phone").value;
        var mobile=document.getElementById("Mobile").value;
        Mask();
        SetAddress("<br /><br />数据加载中......<br /><br /><img src='../Include/Images/loadimg.gif'>");        
        $j.post("../Handler/AddressHandler.ashx", {Reciver:reciver,Province:Province,City:City,Area:Area,Address:daddress,Post:post,Phone:phone,Mobile:mobile, OP:"Add"},
                function(data){                          
                   window.location.reload();
                }
            );
    }
}
function Next()
{
    var reciver=document.getElementById("T_Name");
    if(reciver.value.length<1)
    {
        alert("收货人姓名不能为空");
        reciver.focus();
        return false;
    }
    var Province=document.getElementById("Province");
    if(Province.selectedIndex<1)
    {
        alert("请选择省份");
        Province.focus();
        return false;
    }
    var City=document.getElementById("City");
    if(City.selectedIndex<1)
    {
        alert("请选择城市");
        City.focus();
        return false;
    }
    var Area=document.getElementById("Area");
    if(Area.selectedIndex<1)
    {
        alert("请选择区县");
        Area.focus();
        return false;
    }
    var daddress=document.getElementById("Address");
    if(daddress.value.length<1)
    {
        alert("请填写具体地址");
        daddress.focus();
        return false;
    }
    var post=document.getElementById("Post");
    if(isNaN(post.value))
    {
        alert("邮编含有非法字符");
        post.focus();
        return false;
    }
    if(post.value.length!=6)
    {
        alert("邮编长度非法");
        post.focus();
        return false;
    }
    var phone=document.getElementById("Phone");
    if(isNaN(phone.value))
    {
        alert("固定电话含有非法字符");
        phone.focus();
        return false;
    }
    var mobile=document.getElementById("Mobile");
    if(isNaN(mobile.value))
    {
        alert("手机号码含有非法字符");
        mobile.focus();
        return false;
    }
    if(mobile.value.length!=11)
    {
        alert("手机长度错误");
        mobile.focus();
        return false;
    }
    if(mobile.value.substring(0,2)!='13')
    {
        alert("手机号码格式错误");
        mobile.focus();
        return false;
    }
    var Delivers=document.getElementsByTagName("input");
    var Deliver;
    for(var i=0;i<Delivers.length;i++)
    {
        if(Delivers[i].checked&&Delivers[i].type=="radio")
        {
            Deliver=Delivers[i].value;
        }
    }
    var DeliverTime=document.getElementById("DeliverTime").value;
    var txtMemo=document.getElementById("txtMemo").value;
    $j.post("../Handler/AddressHandler.ashx", {Reciver:reciver.value,Province:Province.value,City:City.value,Area:Area.value,Address:daddress.value,Post:post.value,Phone:phone.value,Mobile:mobile.value,Deliver:Deliver,DeliverTime:DeliverTime,txtMemo:txtMemo, OP:"Next"},
                function(data){             
                   if(data)
                   {             
                       window.location.href="OrderPay.aspx";
                   }
                   else
                   {
                        alert("数据写入失败,请重试");
                   }
                }
            );
}
